﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.JCI.Bll;
using Microsoft.JCI.Common.Entity;
using Microsoft.JCI.Common.Utility;
using System.Text;

namespace Microsoft.JCI.Portal.Dispatch
{
    public partial class BranchWorkItemList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            StringBuilder sbWhereClause = new StringBuilder(" and AllocatedBranch in (");
            sbWhereClause.Append(this.CurrentUserEntity.QueryFilterCompanyCode());
            sbWhereClause.Append(")");

            if (ddlIsCheck.SelectedValue != "2")
            {
                sbWhereClause.Append(" and IsChecked = ");
                sbWhereClause.Append(ddlIsCheck.SelectedValue);
            }
            if (!string.IsNullOrEmpty(txtBranch.Text.Trim()))
            {
                sbWhereClause.Append(" and BranchName like '");
                sbWhereClause.Append("%" + txtBranch.Text.Trim() + "%'");
            }
            if (!string.IsNullOrEmpty(txtFormCode.Text.Trim()))
            {
                sbWhereClause.Append(" and FormCode = '");
                sbWhereClause.Append(txtFormCode.Text.Trim() + "'");
            }
            if (ddlFormType.SelectedValue != "2")
            {
                sbWhereClause.Append(" and WorkingType = ");
                sbWhereClause.Append(ddlFormType.SelectedValue);
            }
            if (!string.IsNullOrEmpty(txtVender.Text.Trim()))
            {
                sbWhereClause.Append(" and VenderName like ' ");
                sbWhereClause.Append("%" + txtVender.Text.Trim() + "%'");
            }
            if (dtFrom.SelectedDate.HasValue && dtTo.SelectedDate.HasValue)
            {
                sbWhereClause.Append(" and CreateOn >= '");
                sbWhereClause.Append(dtFrom.SelectedDate.Value.ToString() + "'");
                sbWhereClause.Append(" and CreateOn <= '");
                sbWhereClause.Append(dtTo.SelectedDate.Value.ToString() + "'");
            }

            DispatchBll bll = new DispatchBll();

            int RecordCount = 0;
            DataTable dt = bll.QueryWorkItems(sbWhereClause.ToString(), AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, ref RecordCount);
            radgridWorkItems.DataSource = dt;
            radgridWorkItems.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void radgridWorkItems_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                Label lblWorkingType = e.Item.FindControl("lblWorkingType") as Label;

                DataRowView entity = e.Item.DataItem as DataRowView;
                if (entity != null)
                {
                    lblWorkingType.Text = EnumeratorHelper.GetFieldText((Enumerator.WorkType)Convert.ToInt32(entity["WorkingType"]));
                }

                HyperLink lnkbtnView = e.Item.FindControl("lnkbtnView") as HyperLink;
                if ((Enumerator.WorkType)Convert.ToInt32(entity["WorkingType"]) == Enumerator.WorkType.BootDebugging)
                {
                    lnkbtnView.NavigateUrl = "WorkItemAdd2.aspx?DispatchFormID=" + entity["DispatchFormId"].ToString() + "&WorkOrderID=" + entity["WorkingVendorOrderId"].ToString();
                }
                if ((Enumerator.WorkType)Convert.ToInt32(entity["WorkingType"]) == Enumerator.WorkType.Maintenance)
                {
                    lnkbtnView.NavigateUrl = "WorkItemAdd.aspx?DispatchFormID=" + entity["DispatchFormId"].ToString() + "&WorkOrderID=" + entity["WorkingVendorOrderId"].ToString();
                }
            }
        }
    }
}
